package zzcltw.com.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import zzcltw.com.object.Enroll;
import zzcltw.com.service.IEnrollService;
import zzcltw.com.util.ExcelPOI07Util;


@RestController
public class EnrollController {
	private static final String U8 = "UTF-8";
	@Autowired
    private IEnrollService enrollService;
	/**
	 * 
	 * @Title getEnrollList
	 * @Description: 招商信息列表查询
	 * @param: @param person	
	 * @return void
	 * @throws
	 * @author zzx
	 * @Date 2018年8月15日   上午11:33:17
	 */
	@RequestMapping(value="/getEnrollList",method = RequestMethod.POST)
    public Map<String,Object> getEnrollList(String enrollNm,String start,String limit){
		Map<String,String> map = new HashMap<>();
    	map.put("enrollNm", enrollNm);  
    	map.put("start", start);
    	map.put("limit", limit);
		return enrollService.queryEnrollList(map);
	}
	/**
	 * 
	 * @Title queryEnrollDtlById
	 * @Description: 报名人员信息详情
	 * @param: @param person	
	 * @return void
	 * @throws
	 * @author zzx
	 * @Date 2018年8月21日   上午11:33:17
	 */
	@RequestMapping(value="/getEnrollDtlInfo",method = RequestMethod.POST)
    public Map<String,Object> queryEnrollDtlById(String enrollId){
		Map<String,String> map = new HashMap<>();
    	map.put("enrollId", enrollId);
		return enrollService.queryEnrollDtlById(map);
	}
	/**
	 * 
	 * @Title delEnrollInfo
	 * @Description: 报名人员信息删除
	 * @param: @param person	
	 * @return void
	 * @throws
	 * @author zzx
	 * @Date 2018年8月21日   上午11:33:17
	 */
	@RequestMapping(value="/delEnrollInfo",method = RequestMethod.POST)
    public Map<String,Object> delEnrollInfo(Enroll enroll){
		return enrollService.delEnrollInfoById(enroll);
	}
	/**
	 * 
	 * @Title saveCompanyInfo
	 * @Description: 新增招商信息
	 * @param: @param person
	 * @return void
	 * @throws Exception
	 * @author zzx
	 * @Date 2018年8月15日   上午11:33:17
	 */
	@RequestMapping(value="/addEnrollInfo", method=RequestMethod.POST)
    public Map<String,Object> saveEnrollInfo(Enroll enroll) throws Exception { 
		return enrollService.saveEnrollInfo(enroll);  
	}
	/**
	 * 
	 * @Title updateEnrollInfo
	 * @Description: 编辑招商信息
	 * @param: @param person
	 * @return void
	 * @throws Exception
	 * @author zzx
	 * @Date 2018年8月15日   上午11:33:17
	 */
	@RequestMapping(value="/updateEnrollInfo", method=RequestMethod.POST)
    public Map<String,Object> updateEnrollInfo(Enroll enroll) throws Exception { 
		return enrollService.updateEnrollInfo(enroll);  
	}
	/**
	 * 
	 * @Title createExcel
	 * @Description: excel 报名人员信息导出
	 * @param: 
	 * @return void
	 * @throws
	 * @author zzx
	 * @Date 2018年3月31日   下午3:18:04
	 */
    @SuppressWarnings("unchecked")
	@RequestMapping(value="/getEnrollListExcel",method = RequestMethod.GET)
	public void getEnrollListExcel(HttpServletResponse response) {
    	Map<String,String> param = new HashMap<>();
    	Map<String,Object> mm = enrollService.queryEnrollList(param);
    	List<Map<String,String>> list = (List<Map<String, String>>) mm.get("beans");
    	Map<String,String> map = new HashMap<>();
		String headCnName = "姓名,电话,地址,QQ,微信,需求描述,推荐人,推荐人电话,会议类型,会议名称";
		String dataEnName = "userNm,userTelnum,enrollAddr,userQq,userWeixin,enrollDesc,introducerPsn,introducerTelnum,enrollTypeCdNm,meetNm";
		String dataSize = "25,25,25,25,25,25,25,25,25,25";
		map.put("excel_head_cnname", headCnName);
		map.put("excel_data_cellSize", dataSize);
		map.put("excel_data_enname", dataEnName);		
		
    	String userAgent = response.getHeader("User-Agent");
		OutputStream outStream = null;
		try {
			boolean isIE = false;
			if (userAgent != null && (userAgent.toLowerCase().contains("msie") || userAgent.toLowerCase().contains("trident"))) {
				isIE = true;
			}
			outStream = response.getOutputStream();//new FileOutputStream(file);			
			String fileName = "报名人员信息"+System.currentTimeMillis()+".xlsx";
			setResponseFileName(isIE, response, fileName);
			ExcelPOI07Util.createExcel2007MergeCells(outStream, map,list);
			outStream.close();
			response.flushBuffer();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}
    public void setResponseFileName(boolean isIE, HttpServletResponse response, String displayName) throws UnsupportedEncodingException  {
		String dspName;
		String dspNameNew;
		if (isIE) {
			dspName = URLEncoder.encode(displayName, U8);
			dspNameNew = dspName.replaceAll("\\+", "%20");// 修正URLEncoder将空格转换成+号的BUG
			response.setHeader("Content-Disposition", "attachment;filename=" + dspNameNew);
		} else {
			 //String enableFileName = "=?UTF-8?B?" + (new String(Base64.encodeBase64(displayName.getBytes("UTF-8")))) + "?=";
			dspNameNew = new String(displayName.getBytes(U8), "ISO8859-1");
			response.setHeader("Content-Disposition","attachment;filename=\"" + dspNameNew + "\"");// firefox空格截断
		}
	}
}
